package org.lex.swt;

import java.lang.reflect.Constructor;

import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.lex.ClassPane;

public class ViewComposite {
	public static void main(String[] args) {
		CompWindow win = new CompWindow(ClassPane.class);
		win.open();
		Display.getCurrent().dispose();
	}

	static class CompWindow extends ApplicationWindow {
		private Class<?> compClz = null;

		public CompWindow(Class<?> compClz) {
			super((Shell) null);
			this.compClz = compClz;
			this.setShellStyle(SWT.MAX | SWT.RESIZE | SWT.MIN);
			this.setBlockOnOpen(true);
		}

		@Override
		protected void configureShell(Shell shell) {
			super.configureShell(shell);
			shell.setText(compClz.getCanonicalName());
			shell.setSize(300, 200);
		}

		@Override
		protected Control createContents(Composite parent) {
			try {
				Constructor<?> cons = compClz.getConstructor(Composite.class);
				if (null != cons) {
					cons.newInstance(parent);
				} else {
					cons = compClz.getConstructor(Composite.class, int.class);
					cons.newInstance(parent, SWT.NONE);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return parent;
		}

	}
}
